/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
  ******************************************************************************
  * @file    serial_task.c
  * @author  GEEKROS,  site:www.geekros.com
  ******************************************************************************
*/
#include "serial_task.h"

static Serial_Data_Struct Read_Data;

#define CALIBRATE_COUNT 1000
#define CALIBRATE_FREQUENCY 5
#define CALIBRATE_AVERAGE_COUNT 100
#define MAGNETIC_FIELD_STRENGTH 0.05    // 磁场波动范围
#define BEEP_ON_FREQUENCY 50
#define BEEP_ON_FREQUENCY 300
#define TASK_DELAY_MS 5
#define BUFF_LEN 20

/******************************************************************
  * @ 函数名  ： Serial_Task
  * @ 功能说明： USB虚拟串口任务函数
  * @ 参数    ： NULL
  * @ 返回值  ： NULL
  *****************************************************************/
static float Mag_z_buff[100] = {0};
void Serial_Task(void)
{
    float Gauss_Mag_z;
    uint32_t i = 0, j = 0, k = 0;
    bool Mag_z_Flag = 0;
    float origin_mag_z = 0;
 
    while (1) {
        char buff[BUFF_LEN] = {0};

        Gauss_Mag_z = Mpu_Calc.mag_z * 0.15f * 0.01f;
        if (i < CALIBRATE_COUNT) {
            i++;
            if (i % CALIBRATE_FREQUENCY == 0) {
                Mag_z_buff[j++] = Gauss_Mag_z;
                if (j >= CALIBRATE_AVERAGE_COUNT) {
                    i = CALIBRATE_COUNT;
                    Mag_z_Flag = true;
                    for (k=0; k<CALIBRATE_AVERAGE_COUNT; k++) {
                        origin_mag_z += Mag_z_buff[k];
                    }
                    origin_mag_z = origin_mag_z / CALIBRATE_AVERAGE_COUNT;
                    // 校准完成，蓝灯亮，发送给应用端 "CalibrateOK"
                    BLUE_LED_ON();
                    memset_s(buff, 0x00, sizeof(buff));
                    sprintf(buff, "angle:%s", "CalibrateOK");
                    ESP8266_send_data(buff, strlen(buff));
                }
            }
        }

        if (Mag_z_Flag) {
            if (fabs(Gauss_Mag_z - origin_mag_z) > MAGNETIC_FIELD_STRENGTH) {
                // 检测到金属，开启蜂鸣器， 发送给应用端
                TIM_SetCompare1(TIM12, BEEP_ON_FREQUENCY);
                memset_s(buff, 0x00, sizeof(buff));
                sprintf_s(buff, "angle:%s", "Detected");
                ESP8266_send_data(buff, strlen(buff));
            } else {
                // 未检测到金属，关闭蜂鸣器， 发送给应用端
                TIM_SetCompare1(TIM12, BEEP_ON_FREQUENCY);
                memset_s(buff, 0x00, sizeof(buff));
                sprintf_s(buff, "angle:%s", "UnDetected");
                ESP8266_send_data(buff, strlen(buff));
            }
        }
        Task_Delay(TASK_DELAY_MS);
    }
}

